package com.hzjq.fxlib.presenter

import android.view.KeyEvent
import android.view.ViewGroup
import com.hzjq.core.BlastDelegate
import com.hzjq.fxlib.R
import com.wxl.common.util.Toll
import kotlinx.android.synthetic.main.activity_blast.*

class ChargeFinishPresenter : AbsViewPresenter {

    private var pressState = arrayListOf(0, 0)
    private var pressTime = 0L
    private var run:Runnable?=null

    constructor(rootView: ViewGroup) : super(rootView)

    override fun onContentViewId(): Int {
        return R.layout.layout_charge_ok
    }

    override fun onResume() {

    }

    override fun showAction(action: String) {

    }

    fun setClickCallback(r:Runnable){
        run = r
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if(keyCode == KeyEvent.KEYCODE_F1 || keyCode == KeyEvent.KEYCODE_F5){//Aa
            onPressKey(0)
        }
        if(keyCode == KeyEvent.KEYCODE_F4 || keyCode == KeyEvent.KEYCODE_F8){//Fn
            onPressKey(1)
        }
        return super.onKeyDown(keyCode, event)
    }


    private fun onPressKey(position: Int){
        val time = System.currentTimeMillis()
        pressState[position] = 1
        if (time - pressTime > 500) {
            BlastDelegate.getDelegate().postDelay(500, Runnable {
                if (pressState[0] == 1 && pressState[1] == 1) {
                    return@Runnable
                }
                pressTime = 0
                pressState[0] = 0
                pressState[1] = 0
                Toll.toll("请同时按下两个起爆按钮")
            })
            pressTime = time
        } else {
            if (pressState[0] == 1 && pressState[1] == 1) {
                run?.run()
            } else {
                Toll.toll("请同时按下两个起爆按钮")
            }
        }
    }
}